package com.exam.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;

import com.exam.util.ConnDB;
import com.opensymphony.xwork2.ActionContext;

abstract class BaseDao {
	public Connection conn= null;
	public PreparedStatement pstm = null;
	public ResultSet res,res2 = null;
	
	public String getAdmin(){
		return (String) ActionContext.getContext().getSession().get("admin"); 
	}
	
	/**
	 * 
	 * Description:查询
	 * @author: 陈维康
	 * @date: 2017年4月19日
	 * @param sql
	 * @return ResultSet
	 */
	public  ResultSet query(String sql) {
		conn = ConnDB.getConn();
		try {
			pstm = conn.prepareStatement(sql);
			res = pstm.executeQuery();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return res;
	}	
	
	/**
	 * 
	 * Description:增删改
	 * @author: 陈维康
	 * @date: 2017年4月19日
	 * @param sql
	 * @return boolean
	 */
	public  boolean adu(String sql) {
			int m=0;
			conn = ConnDB.getConn();
			try {
				pstm = conn.prepareStatement(sql);
				m = pstm.executeUpdate();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			this.closeDB();
			if(m>0) 
				return true;
			else
				return false;
	}  
	
	
	
	/**
	 * ��ݱ����ֻ�ȡ �ܼ�¼
	 * @param tableName
	 * @return
	 */
	public int getCount (String tableName){
		String sql = "select count(*) count from "+tableName;
		Integer count = 0;
		this.query(sql);
		try {
			if(res.next()){
				count = res.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		this.closeDB();
		return count;
	}

	public void closeDB(){
		try {
			if(!this.conn.isClosed()&&this.conn!=null) 
				this.conn.close();
		} catch (SQLException e) {
				e.printStackTrace();
			}
	}
	
}
